package com.topisv.tms.utils.sto;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.ExpressConfigure;
import com.topisv.tms.utils.sto.vo.OrderInfo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MailSTOUtil {
	private static final Logger log = LoggerFactory.getLogger(MailSTOUtil.class);
	
	private static String request_code = "vip0009";
	private static String create_code = "vip0007";
	
	public static List<String> mailNoRequest(ExpressConfigure expressConfigure) throws ClientProtocolException, IOException{
		log.info("----------开始请求申通电子面单-----------");
		List<String> mailnoList = new ArrayList<String>();;
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(expressConfigure.getUrl());
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();   			
			nvps.add(new BasicNameValuePair("code",request_code));
			nvps.add(new BasicNameValuePair("data_digest",expressConfigure.getAppKey()));
			nvps.add(new BasicNameValuePair("cuspwd",expressConfigure.getPassword()));
			nvps.add(new BasicNameValuePair("cusname",expressConfigure.getWarehouseName()));
			nvps.add(new BasicNameValuePair("cusite",expressConfigure.getCompanyName()));
			nvps.add(new BasicNameValuePair("len","500"));
			httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); 
			HttpResponse response = httpclient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if(entity!=null) {
				String str = new String(EntityUtils.toString(entity));
				log.info("----------请求-"+str);
				JSONObject json = JSONObject.fromObject(str);
				if("true".equals(json.getString("success"))){
					String [] mailnos = json.getString("data").split(",");
					for(String mailno : mailnos){
						mailnoList.add(mailno);
					}
					log.info("----------请求成功-----------");
				}else{
					log.info("----------请求失败-----------");
				}
			}
			
			return mailnoList;
		} catch (ParseException e) {
			log.info("----------请求失败-----------");
			e.printStackTrace();
			return null;
		}
	}
	
	public static String sendOrderInfo(EdiOrder e,ExpressConfigure ec) throws ClientProtocolException, IOException{
			JSONArray json = createOrderInfo(e,ec);
			String str = null;
			try {
				log.info("---------开始发送订单信息---------");
				HttpClient httpclient = new DefaultHttpClient();
				HttpPost httpPost = new HttpPost(ec.getUrl());
				List<NameValuePair> nvps = new ArrayList<NameValuePair>();   			
				nvps.add(new BasicNameValuePair("code",create_code));
				nvps.add(new BasicNameValuePair("data_digest",ec.getAppKey()));
				nvps.add(new BasicNameValuePair("cuspwd",ec.getPassword()));
				nvps.add(new BasicNameValuePair("data",json.toString()));
				httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); 
				HttpResponse response = httpclient.execute(httpPost);
				HttpEntity entity = response.getEntity();
				if(entity!=null) {
					str = new String(EntityUtils.toString(entity));
					JSONObject jsons = JSONObject.fromObject(str);
					String  data= jsons.getString("data");
					log.info("STO回传信息"+data);
					if("true".equals(jsons.getString("success"))){
						log.info("---------发送订单信息成功---------");
					}else{
						log.info("---------发送订单信息失败---------");
						log.info("回传信息"+data);
					}
				}
					
			} catch (ParseException e1) {
				log.info("---------发送订单信息失败---------");
				e1.printStackTrace();
			}
			return null;
	}
	/**
	 * 申通电子面单在打印时调用的方法
	 * @param e
	 * @param ec
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String sendOrderInfoForPrint(EdiOrder e,ExpressConfigure ec) throws ClientProtocolException, IOException{
		JSONArray json = createOrderInfo(e,ec);
		String str = null;
		try {
			log.info("---------开始发送订单信息---------");
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(ec.getUrl());
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();   			
			nvps.add(new BasicNameValuePair("code",create_code));
			nvps.add(new BasicNameValuePair("data_digest",ec.getAppKey()));
			nvps.add(new BasicNameValuePair("cuspwd",ec.getPassword()));
			nvps.add(new BasicNameValuePair("data",json.toString()));
			httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); 
			HttpResponse response = httpclient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if(entity!=null) {
				str = new String(EntityUtils.toString(entity));
				JSONObject jsons = JSONObject.fromObject(str);
				String  data= jsons.getString("data");
				
				log.info("STO回传信息"+data);
				if("true".equals(jsons.getString("success"))){
					log.info("---------发送订单信息成功---------");
					return data;
				}else{
					log.info("---------发送订单信息失败---------");
					log.info("回传信息"+data);
					return null;
				}
			}
				
		} catch (ParseException e1) {
			log.info("---------发送订单信息失败---------");
			e1.printStackTrace();
		}
		return null;
   }
	private static JSONArray  createOrderInfo(EdiOrder e,ExpressConfigure ec){
		log.info("----------创建订单信息-----------");
		OrderInfo order = new OrderInfo();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		order.setBillno(e.getMailNo());			
		order.setSenddate(sdf.format(new Date()));
		order.setSendsite(ec.getCompanyName());
		order.setSendcus(ec.getWarehouseName());
		order.setSendperson("");	//寄件人
		order.setSendtel("");
		order.setReceivecus("");
		order.setReceiveperson(e.getRecena());
		order.setReceivetel(e.getMobile()!=null?e.getMobile():e.getTeleph());
		order.setGoodsname("");
		order.setInputdate("");
		order.setInputperson("");
		order.setInputsite("");
		order.setLasteditdate("");
		order.setLasteditperson("");
		order.setLasteditsite("");
		order.setRemark(e.getRemark());
		order.setReceiveprovince(e.getProvna());
		order.setReceivecity(e.getCityna());
		order.setReceivearea(e.getDistna());
		order.setReceiveaddress(e.getAddres());
		order.setSendprovince("");
		order.setSendcity("");
		order.setSendarea("");
		order.setSendaddress("");
		order.setWeight("");
		order.setProductcode("");
		order.setSendpcode("");
		order.setSendccode("");
		order.setSendacode("");
		order.setReceivepcode("");
		order.setReceiveccode("");
		order.setReceiveacode("");
		StringBuffer bigChar = new StringBuffer("");
		bigChar.append(e.getProvna()).append(e.getCityna());
		order.setBigchar(bigChar.toString());
		order.setOrderno(e.getOrdeno());
		JSONArray json = JSONArray.fromObject(order);
		log.info("----------创建订单信息成功-----------");
		log.info("json信息："+json.toString());
		return json;
		
	}

	public static void main(String[] args) throws ClientProtocolException, IOException {
//		HttpClient httpclient = new DefaultHttpClient();
//		OrderInfo o = new OrderInfo();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//		o.setBillno("220000000120");
//		o.setSenddate(sdf.format(new Date()));
//		o.setSendsite("上海陈行公司");
//		o.setSendcus("东商");
//		o.setSendperson("");
//		o.setSendtel("");
//		o.setReceivecus("");
//		o.setReceiveperson("");
//		o.setReceivetel("2313212312");
//		o.setGoodsname("空调");
//		o.setInputdate("");
//		o.setInputperson("东商");
//		o.setInputsite("上海陈行公司");
//		o.setLasteditdate("");
//		o.setLasteditperson("");
//		o.setLasteditsite("");
//		o.setRemark("测试备注");
//		o.setReceiveprovince("广东省");
//		o.setReceivecity("深圳市");
//		o.setReceivearea("南山区");
//		o.setReceiveaddress("收件地址");
//		o.setSendprovince("湖南省");
//		o.setSendcity("郴州市");
//		o.setSendarea("永兴县");
//		o.setSendaddress("寄件地址");
//		o.setWeight("12");
//		o.setProductcode("");
//		o.setSendpcode("");
//		o.setSendccode("");
//		o.setSendacode("");
//		o.setReceivepcode("");
//		o.setReceiveccode("");
//		o.setReceiveacode("");
//		o.setBigchar("广东省深圳市");
//		o.setOrderno("PD123456789");
//		JSONArray json = JSONArray.fromObject(o);
//		System.out.println(json.toString());
//		HttpPost httpPost = new HttpPost("http://183.16.159.77:9098/msd_vip/PreviewInterfaceAction.action");
//		List<NameValuePair> nvps = new ArrayList<NameValuePair>();   			
//		nvps.add(new BasicNameValuePair("code","vip0007"));
//		nvps.add(new BasicNameValuePair("data_digest","ec30c4dd6d04325b72688384753c2952"));
//		nvps.add(new BasicNameValuePair("cuspwd","123456"));
//		nvps.add(new BasicNameValuePair("data",json.toString()));
//		httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); 
//		HttpResponse response = httpclient.execute(httpPost);
//		HttpEntity entity = response.getEntity();
//		if(entity!=null) {
//		String str = new String(EntityUtils.toString(entity));
//		System.out.println(str);
//		}
//	}
		
  //请求面单
//	HttpClient httpclient = new DefaultHttpClient();
//	HttpPost httpPost = new HttpPost("http://vip.sto.cn/PreviewInterfaceAction.action");
//	List<NameValuePair> nvps = new ArrayList<NameValuePair>();   			
//	nvps.add(new BasicNameValuePair("code","vip0009"));
//	nvps.add(new BasicNameValuePair("data_digest","a2448cc82d1140ef11428b95aebb5f85"));
//	nvps.add(new BasicNameValuePair("cusname","派联仓储"));
//	nvps.add(new BasicNameValuePair("cusite","北京顺义公司"));
//	nvps.add(new BasicNameValuePair("cuspwd","3e3e8b29a9c2854e601ba43f360bfbaf"));
//	nvps.add(new BasicNameValuePair("len","1"));
//	httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); 
//	HttpResponse response = httpclient.execute(httpPost);
//	HttpEntity entity = response.getEntity();
//		if(entity!=null) {
//			String str = new String(EntityUtils.toString(entity));
//			System.out.println(str);
//			log.info(str);
//		}
	}
}
